package com.assets.Check.newTanWanCheck;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.assets.R;
import com.assets.databinding.ItemCheckBinding;

import java.util.ArrayList;

/**
 * @作者(author): WR
 * @创建时间(date): 2021/5/20
 */
public class CheckAdapter extends RecyclerView.Adapter<CheckAdapter.CheckViewHolder> {

    private Context context;
    private ArrayList<ItemCheckData> dataList;

    public CheckAdapter(Context context, ArrayList<ItemCheckData> dataList) {
        this.context = context;
        this.dataList = dataList;
    }

    @NonNull
    @Override
    public CheckViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_check, parent, false);
        return new CheckViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CheckViewHolder holder, int position) {
        holder.binding.tvTitle.setText(dataList.get(position).getTitle()+":");
        holder.binding.tvContent.setText(dataList.get(position).getContent());
        holder.binding.tvName.setText(dataList.get(position).getName());
        holder.binding.tvNum.setText(dataList.get(position).getNum());
        holder.binding.tvStatus.setText(dataList.get(position).getStatus());
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public interface Request {
        void result(int position);
    }

    private Request request;

    public void setRequest(Request request) {
        this.request = request;
    }

    class CheckViewHolder extends RecyclerView.ViewHolder {
        private ItemCheckBinding binding;


        CheckViewHolder(@NonNull View itemView) {
            super(itemView);
            binding = DataBindingUtil.bind(itemView);
            itemView.setOnClickListener(v -> {
                if (request != null) {
                    request.result(getAdapterPosition());
                }
            });
        }
    }
}
